#include "Common.h"
#include "SceneRenderTargets.h"
#include "MultiRenderTarget.h"


SceneRenderTargets::SceneRenderTargets()
	: mBufferSizeX(0)
	, mBufferSizeY(0)
	, mFilterDownsampleFactor(4)
	, mFilterBufferSizeX(0)
	, mFilterBufferSizeY(0)
	, mBaseTargets(NULL)
{
}

SceneRenderTargets::~SceneRenderTargets()
{
	Release();
}

void SceneRenderTargets::Allocate(int mx, int my)
{
	if (mBufferSizeX < mx || mBufferSizeY < my)
	{
		// Reinitialize the render targets for the given size.
		SetBufferSize(Max(mBufferSizeX, mx), Max(mBufferSizeY, my));

		Release();
		Init();
	}
}

void SceneRenderTargets::SetBufferSize(int x, int y)
{
	mBufferSizeX = x;
	mBufferSizeY = y;
	mFilterDownsampleFactor = 4;
	mFilterBufferSizeX = mBufferSizeX / mFilterDownsampleFactor + 2;
	mFilterBufferSizeY = mBufferSizeY / mFilterDownsampleFactor + 2;
}

void SceneRenderTargets::Init()
{
	if (mBufferSizeX <= 0 || mBufferSizeY <= 0)
		return;

	mBaseTargets = new MultiRenderTarget(mBufferSizeX, mBufferSizeY, D3DFMT_A16B16G16R16F, false, 4);
	mBaseTargets->Init();
}

void SceneRenderTargets::Release()
{
	if (mBaseTargets != NULL)
	{
		delete mBaseTargets;
		mBaseTargets = NULL;
	}
}

void SceneRenderTargets::BeginBasePass()
{
	mBaseTargets->Attach();
}